package com.qijia.travel.app;

import java.util.List;

import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.Tream;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.TreamExample;

public class TreamManager {
	private BaseDAO baseDao;

	public TreamManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}

	public Tream getTreamById(int treamId) throws AppErrorException {
		Tream tream = new Tream();
		tream.setTreamId(treamId);

		try {
			return (Tream) baseDao.selectByPrimaryKey(SqlMappedId.TREAM + "."
					+ SqlMappedId.selectByPrimaryKey, tream);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取导游基本信息失败!");
		}
	}

	public void createTream(Tream tream) throws AppErrorException {
		if (tream == null)
			throw new AppErrorException("请输入新增的团队的基本信息");
		try {
			if (tream.getTreamName() != null)
				baseDao.insert(SqlMappedId.TREAM + "." + SqlMappedId.insert,
						tream);
		} catch (TravelDbException e) {
			throw new AppErrorException("注册新团队失败,请稍候重试!", e);
		}
	}

	public void updateTream(Tream tream) throws AppErrorException {
		if (tream == null)
			throw new AppErrorException("请选择要进行修改的团队");
		try {
			baseDao.updateByPrimaryKeySelective(SqlMappedId.TREAM + "."
					+ SqlMappedId.updateByPrimaryKeySelective, tream);
		} catch (TravelDbException e) {
			e.printStackTrace();
			throw new AppErrorException("更新选中的团队[" + tream.getTreamName()
					+ "]失败,请稍候重试!", e);
		}
	}

	public void deleteTream(int treamId) throws AppErrorException {
		if (treamId == 0)
			throw new AppErrorException("请选择要进行删除的团队");
		Tream tream = new Tream();
		try {
			tream.setTreamId(treamId);
			baseDao.deleteByPrimaryKey(SqlMappedId.TREAM + "."
					+ SqlMappedId.deleteByPrimaryKey, tream);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的团队[编号为:" + tream.getTreamId() + "]失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Tream> getTreamByExample() throws AppErrorException {
		TreamExample example = ExampleFactory.createTreamExample();
		example.createCriteria();
		try {
			return (List<Tream>) baseDao.selectByExample(SqlMappedId.TREAM
					+ "." + SqlMappedId.selectByExample, example);
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的团队失败,请稍候重试!", e);
		}
	}

	public PageResult selectTreamList(int curPage) throws AppErrorException {
		TreamExample example = ExampleFactory.createTreamExample();
		example.createCriteria();
		example.setOrderByClause(" tream_id ");

		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.TREAM + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.TREAM + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (curPage <= 1)
				curPage = 1;

			page.setCurPage(curPage);
			page.setPageSize(10);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取团队数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取团队数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取团队数据列表失败,请稍候重试!", e);
		}
	}
}
